package com.yangye.pidoublename.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.yangye.picommon.exception.PiException;
import com.yangye.pidoublename.service.DoubleNameService;
import com.yangye.pidoublename.service.ProvinceEnum;
import com.yangye.pidoublename.vo.DoubleNameVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

/**
 * @author yangye
 * @date 2019/9/19
 */
@Service
public class ChongQingServiceImpl implements DoubleNameService {

	@Autowired
	private RestTemplate restTemplate;

	private final String QUERY_URL = "http://wsga.gaj.cq.gov.cn/apis//querys/wxname/";
	@Override
	public DoubleNameVo findByName(String name) {
		JSONObject result = restTemplate.getForObject(QUERY_URL + name, JSONObject.class);
		if (result.getInteger("code") == 200) {
			JSONObject data = result.getJSONObject("data");
			Integer total = 0;
			if (data != null) {
				total = data.getInteger("cCount");
			}
			DoubleNameVo doubleNameVo = new DoubleNameVo();
			doubleNameVo.setProvince(ProvinceEnum.CHONGQING.getName());
			doubleNameVo.setName(name);
			doubleNameVo.setTotal(total);
			return doubleNameVo;
		} else {
			throw new PiException(ProvinceEnum.CHONGQING.getName() + "请求接口失败");
		}
	}
}
